﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using permit.App_Code.DataAccess;

namespace permit.PollingTable
{
    public partial class printTable : System.Web.UI.Page
    {
        string strTitle;
        int topMove = 190;
        int pageIndex = 0;
        int top;
        int left;
        PrinterAccess priAcc = new PrinterAccess();


        protected void Page_Load(object sender, EventArgs e)
        {

            lblDep.Text = getTitle(Request.QueryString["dep"], Request.QueryString["data"]);


            Table table = new Table();

            DataSet ds = (DataSet)Session["PrintTable"];


            TableRow tr = setColumn(ds.Tables[0].Columns);
            tr.Font.Size = Convert.ToInt32(priAcc.GetPrintLable().Tables[0].Rows[1][4]);
            table.Rows.Add(tr);
            int count = 0;
            fillTable(ds, table, count);

            DataSet dstitle = priAcc.GetPrintLable();

            dstitle = priAcc.GetPrintLable();
            for (int i = 0; i < dstitle.Tables[0].Rows.Count; i++)
            {
                if (i == 0)
                {
                    lblDep.Style["left"] = dstitle.Tables[0].Rows[i][1].ToString() + "mm";
                    lblDep.Style["top"] = dstitle.Tables[0].Rows[i][2].ToString() + "mm";
                }
                if (i == 1)
                {
                    top = Convert.ToInt32(dstitle.Tables[0].Rows[i][2]);
                    left = Convert.ToInt32(dstitle.Tables[0].Rows[i][1]);
                }
            }


            //Page.RegisterStartupScript("bb", "<script language=javascript>printReport();</script>");
            //Page.RegisterStartupScript("aa", "<script language=javascript>printTure();</script>");


        }

        //添加标题
        public string getTitle(string dep, string date)
        {
            if (dep.Contains("市"))
            {
                strTitle = dep.Substring(0, dep.IndexOf("市") + 1) + "局";
            }
            if (dep.Contains("区"))
            {
                strTitle = dep.Substring(0, dep.IndexOf("区") + 1);
            }
            if (dep.Contains("县"))
            {
                strTitle = dep.Substring(0, dep.IndexOf("县") + 1);
            }
            string[] strDate = date.Split('-');
            strTitle = strTitle + strDate[0] + "年" + strDate[1] + "月" + "餐饮服务许可证发放登记表";
            return strTitle;
        }

        //添加列名
        public TableRow setColumn(DataColumnCollection dcc)
        {
            TableRow tr = new TableRow();
            tr.Style["Height"] = "13mm";
            tr.HorizontalAlign = HorizontalAlign.Center;
            tr.Font.Size = Convert.ToInt32(priAcc.GetPrintLable().Tables[0].Rows[1][4]);
            TableCell tcNum = new TableCell();
            tcNum.Text = "序号";
            tr.Cells.Add(tcNum);

            foreach (DataColumn dc in dcc)
            {
                TableCell tc = new TableCell();

                switch (dc.ColumnName)
                {
                    case "per_PermitCode":
                        tc.Text = "许可证号";
                        break;
                    case "per_Department":
                        tc.Text = "单位名称";
                        break;
                    case "per_Address":
                        tc.Text = "地址";
                        break;
                    case "per_Representative":
                        tc.Text = "负责人";
                        break;
                    case "per_Tel":
                        tc.Text = "联系电话";
                        break;
                    case "per_Area":
                        tc.Text = "经营面积";
                        break;
                    case "per_Type":
                        tc.Text = "类别";
                        break;
                    case "per_Number":
                        tc.Text = "从业人数";
                        break;
                    case "per_PermitDate":
                        tc.Text = "发证日期";
                        break;
                    case "per_EndDate":
                        tc.Text = "有效日期";
                        break;
                    case "per_Mark":
                        tc.Text = "备注";
                        break;
                    case "per_Scope":
                        tc.Text = "经营范围";
                       break;


                }
                tr.Cells.Add(tc);
            }
            return tr;
        }

        public void fillTable(DataSet ds, Table table, int count)
        {
            for (int index = count; index < ds.Tables[0].Rows.Count; index++)
            {
                if (count == 11)
                {
                    NewTable(ds, count);
                    break;

                }
                if ((count - 11) % 13 == 0)
                {
                    setTableStyle(table, topMove);
                    print.Controls.Add(table);
                    NewTable(ds, count);
                    break;
                }
                TableRow trData = new TableRow();
                TableCell tc = new TableCell();
                count++;
                tc.Text = count.ToString();
                trData.Cells.Add(tc);
                for (int i = 0; i < ds.Tables[0].Columns.Count; i++)
                {

                    TableCell tcData = new TableCell();
                    if (i == 3)
                    {
                        tcData.Text = ds.Tables[0].Rows[index][i].ToString().Split('（')[0];

                    }
                    else
                    {
                        tcData.Text = ds.Tables[0].Rows[index][i].ToString();
                    }
                    trData.Cells.Add(tcData);
                    if (i == 8 || i == 9)
                    {
                        tcData.Text = ((DateTime)ds.Tables[0].Rows[index][i]).ToShortDateString();
                    }
                   
                }
               
                trData.Style["Height"] = "13mm";

                trData.HorizontalAlign = HorizontalAlign.Center;
                trData.Font.Size = Convert.ToInt32(priAcc.GetPrintLable().Tables[0].Rows[1][4]); ;
                table.Rows.Add(trData);
                
                if (index == ds.Tables[0].Rows.Count - 1 || count == 11)
                {
                    setTableStyle(table, topMove);
                    print.Controls.Add(table);
                }

            }


        }

        public void setTableStyle(Table table, int topMove)
        {

            table.Style["position"] = "absolute";
            if (pageIndex == 1)
            {
                topMove = 163;
            }
            if (pageIndex == 0)
            {
                topMove = 0;
                DataSet dstitle = priAcc.GetPrintLable();

                dstitle = priAcc.GetPrintLable();
                for (int i = 0; i < dstitle.Tables[0].Rows.Count; i++)
                {
                    if (i == 1)
                    {
                        top = Convert.ToInt32(dstitle.Tables[0].Rows[i][2]);
                        left = Convert.ToInt32(dstitle.Tables[0].Rows[i][1]);
                        table.Style["Width"] = dstitle.Tables[0].Rows[i][3].ToString() + "mm";
                    }
                }
            }
            top = top + topMove;
            table.Style["top"] = top.ToString() + "mm";
            table.Style["left"] = left.ToString() + "mm";
            
            //272

            table.GridLines = GridLines.Both;
            table.BorderWidth = 1;
            table.BorderStyle = BorderStyle.Solid;
            table.CellPadding = 0;
            table.CellSpacing = 0;
        }


        public void NewTable(DataSet ds, int count)
        {
            Table tableNew = new Table();
            pageIndex++;

            TableRow tr = setColumn(ds.Tables[0].Columns);
            tableNew.Rows.Add(tr);

            TableRow trData = new TableRow();
            TableCell tc = new TableCell();
            tc.Text = (count + 1).ToString();

            trData.Cells.Add(tc);
            for (int i = 0; i < ds.Tables[0].Columns.Count - 1; i++)
            {
                TableCell tcData = new TableCell();
                if (i == 3)
                {
                    tcData.Text = ds.Tables[0].Rows[count][i].ToString().Split('（')[0];

                }
                else
                {
                    tcData.Text = ds.Tables[0].Rows[count][i].ToString();
                }
                if (i == 8 || i == 9)
                {
                    tcData.Text = ((DateTime)ds.Tables[0].Rows[count][i]).ToShortDateString();
                }
                trData.Cells.Add(tcData);
            }
            
            trData.Style["Height"] = "13mm";
            trData.HorizontalAlign = HorizontalAlign.Center;
            trData.Font.Size = Convert.ToInt32(priAcc.GetPrintLable().Tables[0].Rows[1][4]);
            tableNew.Rows.Add(trData);
            if (count == ds.Tables[0].Rows.Count - 1)
            {
                setTableStyle(tableNew, topMove);
                print.Controls.Add(tableNew);
                return;
            }
            fillTable(ds, tableNew, ++count);
        }

    }
}
